package com.golxen.framework.event.condition;

import com.golxen.framework.event.condition.dao.entity.ConditionInfo;
import com.golxen.framework.event.eGamePlayerEventType;
import com.golxen.framework.manager.ManagerPlugin;
import com.golxen.framework.module.ModulePlugin;
import com.golxen.framework.user.player.GamePlayer;
import com.golxen.utils.LogUtil;

import java.util.HashMap;
import java.util.Map;

public class ConditionPlugin {

    static ConditionPlugin instance = null;

    public static void start() {
        if (instance != null) {
            return;
        }

        instance = new ConditionPlugin();

        ModulePlugin.registModule(ConditionModule.class);

        ManagerPlugin.registMgr(ConditionMgr.class);

    }

    public static boolean active() {
        return instance != null;
    }

    static final Map<Integer, IConditionData> taskConditionFactory = new HashMap<Integer, IConditionData>();
    static final Map<Integer, IConditionData> activityConditionFactory = new HashMap<Integer, IConditionData>();

    /**
     * 注册相当于准备好这个条件类，在各个需要用到新条件的地方调用，放进taskConditionFactory里；
     * 因为我们事先不知道有哪些BaseCondition，BaseCondition是要被继承的 在各个模块注册后才知道继承后的是哪一个，然后再在用户模块统一遍历使用
     * 在各个插件里注册
    **/
    public static void registCommonCondition(eGamePlayerEventType type, IConditionData fn) {
        IConditionData put = taskConditionFactory.put(type.getValue(), fn);
        if (put != null) {
            LogUtil.CONSOLE.error("event type {} registCommon factory muti", type);
            System.exit(1);
        }
    }
    public static void registActivityCondition(eGamePlayerEventType type, IConditionData fn) {
        IConditionData put = activityConditionFactory.put(type.getValue(), fn);
        if (put != null) {
            LogUtil.CONSOLE.error("event type {} registActivity factory muti", type);
            System.exit(1);
        }
    }
    //获得实例后把用户的完成条件信息放入事先准备好的BaseCondition（被继承后的）条件然后返回使用，如果不注册后在创建那根本不知道是谁继承了，使用的是父类
    public static BaseCondition createCommonCondition(GamePlayer player, ConditionInfo conditionInfo, IUserConditionData userConditionData) {
        IConditionData function = taskConditionFactory.get(conditionInfo.getType());

        if (function == null) {
            LogUtil.COMMON.warn("common condition factory not exist type = {}", conditionInfo.getType());
            return null;
        }
        return function.apply(player, conditionInfo, userConditionData);
    }

    public static BaseCondition createActivityCondition(GamePlayer player, ConditionInfo conditionInfo, IUserConditionData userConditionData) {
        IConditionData function = activityConditionFactory.get(conditionInfo.getType());

        if (function == null) {
            LogUtil.COMMON.warn("activity condition factory not exist type = {}", conditionInfo.getType());
            return null;
        }
        return function.apply(player, conditionInfo, userConditionData);
    }

}
